package com.sicheng.lc.周赛.分类.图论;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/8/7 20:29
 */
public class 受限条件下可到达节点的数目 {
    //https://leetcode.cn/problems/reachable-nodes-with-restrictions/
    public int reachableNodes(int n, int[][] edges, int[] restricted) {
        ArrayList<Integer>[] g = new ArrayList[n];
        Arrays.setAll(g, k -> new ArrayList<>(10));
        boolean[] vis = new boolean[n];
        for (int i : restricted) {
            vis[i] = true;
        }


        int res = 0;
        for (int[] edge : edges) {
            int x = edge[0];
            int y = edge[1];
            g[x].add(y);
            g[y].add(x);
        }
        ArrayDeque<Integer> deque = new ArrayDeque<>(n);
        deque.offer(0);
        while (!deque.isEmpty()) {
            int x = deque.poll();
            if (!vis[x]) {
                vis[x] = true;
                res++;
                for (int y : g[x]) {
                    if (vis[y])
                        continue;
                    deque.offer(y);
                }
            }
        }

        return res;

    }


}
